package com.test.design_pattern.proxy.demo3;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyProxy implements InvocationHandler{
    private Object target;//被代理对象

    public MyProxy(Object target){
        this.target = target;
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("检测身份合法性");
        Object res = method.invoke(target, args);
        return res;
    }

    public static void main(String[] args) {
        Door door = new DoorImpl();
        MyProxy myProxy = new MyProxy(door);
        Door proDoor = (Door)Proxy.newProxyInstance(door.getClass().getClassLoader(), door.getClass().getInterfaces(), myProxy);
        proDoor.enter();
    }
}